<?php
require_once "includes/config.php";
require_once "includes/functions.php";
if(isset($_REQUEST['pid'])) {
	$pId=$_REQUEST['pid'];
}		
	$id = $pId;
	$userid=$_REQUEST['user'];
	$duration=$_REQUEST['duration'];
	if($userid=="") {
		echo "<font style='color:red;'>Sign in and add it to your list.</font>";
	}
	elseif($id!="" && $userid!="") {			
			$qry="SELECT * FROM tbl_items where product_id='$id' and seller_id='$userid'";
			$res=mysql_query($qry);
			if(mysql_num_rows($res) > 0) {			
				echo "Already Added.";
			}
			else {
			    $sqlSelProd="SELECT * FROM tbl_product WHERE product_id='$id'";
				$rstSelProd=mysql_query($sqlSelProd);
				$rowSelProd=mysql_fetch_array($rstSelProd);	
				$productCategory=$rowSelProd['category_id'];
				$productOwner=$rowSelProd['posted_by'];
				$productDescription=$rowSelProd['product_description'];
				$productPrice=$rowSelProd['product_price'];
				$productCommission=$rowSelProd['commission'];
				$productQuantity=$rowSelProd['quantity'];
				$productRemarks=$rowSelProd['remarks'];
			    $currentDate=date("Y-m-d");	
				$bargainDuration = date("Y-m-d G:i:s",time() + ($duration * 24 * 60 * 60));
				$productOwnerType=getUserType($productOwner);
				
				$sql="INSERT INTO tbl_items set 
				product_id='$id', 
				category_id='$productCategory',
				seller_id='$userid',
				owner_id=$productOwner,
				owner_type=$productOwnerType, 
				commission='$productCommission', 
				price='$productPrice', 
				product_description='$productDescription',
				remarks='$productRemarks',
			 	quantity='$productQuantity',
				status=1,
				bargain_duration='$bargainDuration', 
				created_date='$currentDate' ";
				$rst=mysql_query($sql);
				$item_id=mysql_insert_id();
				$sqlSelImage="SELECT * FROM tbl_product_images where product_id ='$id' AND product_type='p'";
		$rstSelImage=mysql_query($sqlSelImage);
		$rowSelImage=mysql_fetch_array($rstSelImage);
		$sqlInsImg = "INSERT INTO tbl_product_images SET 
		product_id='$item_id',
		product_type='i',
		product_image_1 ='".$rowSelImage['product_image_1']."'	,
		product_image_2 ='".$rowSelImage['product_image_2']."'	,	
		product_image_3 ='".$rowSelImage['product_image_3']."'	, 	
		product_image_4 ='".$rowSelImage['product_image_4']."'	, 	
		product_image_5 ='".$rowSelImage['product_image_5']."'	, 	
		product_sample_1 ='".$rowSelImage['product_sample_1']."'	, 	
		product_sample_2 ='".$rowSelImage['product_sample_2']."'	, 	
		product_sample_3 ='".$rowSelImage['product_sample_3']."'	, 	
		product_sample_4  ='".$rowSelImage['product_sample_4']."'	,	
		product_sample_5  ='".$rowSelImage['product_sample_5']."'	,	
		product_thumbnail_1 ='".$rowSelImage['product_thumbnail_1']."'	, 	
		product_thumbnail_2 ='".$rowSelImage['product_thumbnail_2']."'	, 	
		product_thumbnail_3 ='".$rowSelImage['product_thumbnail_3']."'	, 	
		product_thumbnail_4 ='".$rowSelImage['product_thumbnail_4']."'	, 	
		product_thumbnail_5 ='".$rowSelImage['product_thumbnail_5']."'";
		$rstInsImg = mysql_query($sqlInsImg);
		
				if($rst) { echo "Product added to your list."; }
				else { echo "<font style='color:red;'>Error on adding the product, Try again.</font>"; }						
					
		}	
		}
				
	?>